package com.baihan.angus.app.game.busshow.adps;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.basic.base.BaseAdp;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.common.bean.response.SceneRenderResponse;

public class SceneRenderAdp extends BaseAdp<SceneRenderResponse.DataX.SceneData.Data, SceneRenderAdp.ViewHolder> {

    private OnItemClickListener mOnItemClickListener;

    @Override
    protected RecyclerView.ViewHolder createYourViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_scene_render, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void bindYourViewHolder(@NonNull ViewHolder holder, int position) {
        ViewHolder vh = holder;

        SceneRenderResponse.DataX.SceneData.Data sceneData = getItem(position);

        vh.tvTitle.setText(sceneData.getTitle());
        vh.tvContent.setText(sceneData.getContent());

        if (sceneData.getRes_type() == 0) {//资源类型 0图片 1文本 2音乐 3视频
            vh.tvPlay.setText("切换");
        } else {
            vh.tvPlay.setText("播放");
        }

        if (sceneData.isSelected()) {
            vh.viewBorder.setVisibility(View.VISIBLE);
            vh.tvPlay.setBackgroundResource(R.mipmap.ic_btn_red);
        } else {
            vh.viewBorder.setVisibility(View.GONE);
            vh.tvPlay.setBackgroundResource(R.mipmap.ic_btn_green);
        }

        vh.tvPlay.setOnClickListener(view -> {
            for (int i = 0; i < mData.size(); i++) {
                if (mData.get(i).isSelected()) {
                    mData.get(i).setSelected(false);
                }
            }
            sceneData.setSelected(!sceneData.isSelected());
            notifyDataSetChanged();

            mOnItemClickListener.onPlay(position);
        });
    }

    @Override
    protected int getEmptyView() {
        return 0;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvTitle, tvPlay, tvContent;
        FrameLayout flItem;
        View viewBorder;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvPlay = itemView.findViewById(R.id.tvPlay);
            tvContent = itemView.findViewById(R.id.tvContent);
            flItem = itemView.findViewById(R.id.flItem);
            viewBorder = itemView.findViewById(R.id.viewBorder);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onPlay(int position);
    }
}

